#include "send_data.h"
//json组包 (给服务器发送数据让服务器判断这个是否是循环发送)
char* json_pack(USE* user_info) {//传过来是指针了!!!
    cJSON* json = cJSON_CreateObject();
    cJSON_AddNumberToObject(json, "user_id", user_info->user_id);
    cJSON_AddStringToObject(json, "user_name", user_info->user_name);
    cJSON_AddStringToObject(json, "user_password", user_info->user_password);
    cJSON_AddNumberToObject(json, "user_type", user_info->user_type);
    cJSON_AddNumberToObject(json, "type", user_info->type);
    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    return dest;
}


//json组包 (给服务器发送具体的数据)
char* json_pack_data(DAT *data) {//传过来是指针了!!!
    cJSON* json = cJSON_CreateObject();
    cJSON_AddNumberToObject(json, "type", data->type);
    cJSON_AddNumberToObject(json, "data_type", data->data_type);
    cJSON_AddNumberToObject(json, "temperature", data->temperature);
    cJSON_AddNumberToObject(json, "liquid_level", data->liquid_level);
    cJSON_AddNumberToObject(json, "flow_rate", data->flow_rate);
    cJSON_AddNumberToObject(json, "valve_state", data->valve_state);
    cJSON_AddNumberToObject(json, "negative_pressure", data->negative_pressure);
    cJSON_AddNumberToObject(json, "pressure", data->pressure);
    cJSON_AddStringToObject(json, "time", data->time);
    cJSON_AddNumberToObject(json, "pump_id", data->pump_id);

    char* dest = cJSON_Print(json);
    return dest;
}


// 发送详情
int send_details(DAT *detail,int sock){
    int ret = 0;
    char *dest = NULL;
    printf("到json前\n");
    dest = json_pack_data(detail);
    puts(dest);
    ret = send(sock, dest, 1024, 0);
    if (ret == -1) {
        perror("send");
        exit(-1);
    }
    printf("发送成功\n");
}


// 接收数据
//解析json
DAT unpack_details(int sock) {
    DAT dat;
    memset(&dat,0,sizeof(dat));
    static int ret = 0;
    char buf[1024] = { 0 };
    ret = recv(sock, buf, 1024, 0);
    if(ret == -1){
        perror("recv");
        exit(-1);
    }
    printf("%s\n", buf);

    cJSON* json = NULL;
    json = cJSON_Parse(buf);
    if (json == NULL) {
        printf("接收解析error\n");
    }
    cJSON *node = NULL;

    node = cJSON_GetObjectItem(json,"temperature");
    if(node == NULL)
    {
        printf("can't find temperature\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        dat.temperature = node->valuedouble;
    }


    node = cJSON_GetObjectItem(json,"liquid_level");
    if(node == NULL)
    {
        printf("can't find liquid_level\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        dat.liquid_level = node->valuedouble;
    }


    node = cJSON_GetObjectItem(json,"flow_rate");
    if(node == NULL)
    {
        printf("can't find flow_rate\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        dat.flow_rate = node->valuedouble;
    }

    node = cJSON_GetObjectItem(json,"flow_rate");
    if(node == NULL)
    {
        printf("can't find flow_rate\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        dat.flow_rate = node->valuedouble;
    }


    node = cJSON_GetObjectItem(json,"valve_state");
    if(node == NULL)
    {
        printf("can't find valve_state\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        dat.valve_state = node->valueint;
    }





    node = cJSON_GetObjectItem(json,"negative_pressure");
    if(node == NULL)
    {
        printf("can't find negative_pressure\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        dat.negative_pressure = node->valuedouble;
    }



    node = cJSON_GetObjectItem(json,"pressure");
    if(node == NULL)
    {
        printf("can't find pressure\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        dat.pressure= node->valuedouble;
    }

    node = cJSON_GetObjectItem(json, "time");
    if (node == NULL) {
        printf("can't find time\n");
        exit(-1);
    }
    if (node->type == cJSON_String) {
        strcpy(dat.time,node->valuestring);
    }
    return dat;
}
